package com.travel.server.manager.impl.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/27 10:50
 * @Desc:
 */
@Slf4j
public class SmsSal {

    public static String addTemplate(String templateName, String sign, String content, String type){
        JSONObject data = getBaseJson();
        data.put("type", type);
        data.put("template_name", templateName);
        data.put("autograph", sign);
        data.put("content", content);
        JSONObject result = sendPost("https://open.ucpaas.com/ol/sms/addsmstemplate", data);
        return result.getString("templateid");
    }

    public static void updateTemplate(String templateId, String templateName, String sign, String content, String type){
        JSONObject data = getBaseJson();
        data.put("type", type);
        data.put("template_name", templateName);
        data.put("autograph", sign);
        data.put("content", content);
        data.put("templateid", templateId);
        sendPost("https://open.ucpaas.com/ol/sms/editsmstemplate", data);
    }

    public static void sendSms(String templateId, String param, String mobile){
        JSONObject data = getBaseJson();
        data.put("templateid", templateId);
        if(param != null) {
            data.put("param", param);
        }
        data.put("mobile", mobile);
        sendPost("https://open.ucpaas.com/ol/sms/sendsms", data);
    }

    public static void deleteSms(String templateId){
        JSONObject data = getBaseJson();
        data.put("templateid", templateId);
        sendPost("https://open.ucpaas.com/ol/sms/deletesmstemplate", data);
    }

    private static JSONObject sendPost(String url, JSONObject data){
        Map<String, String> param = new HashMap<>();
        param.put("Content-Type", "application/json");
        try {
            log.info("云之讯短信请求 {}", JSONObject.toJSONString(data));
            String result = HttpUtils.doPost(url, data.toJSONString(), 10 * 1000, param);
            log.info("云之讯短信返回 {}", result );
            JSONObject resultData = JSON.parseObject(result);
            if(!"000000".equals(resultData.getString("code"))){
                WorkCode workCode = WorkCode.OTHER_SYSTEM;
                workCode.setMessage(resultData.getString("msg"));
                throw new WorkException(workCode);
            }
            return resultData;
        }catch (WorkException e){
            throw e;
        }
        catch (Exception e){
            log.error("发送短信接口请求异常：", e);
            WorkCode workCode = WorkCode.OTHER_SYSTEM;
            workCode.setMessage("短信接口请求异常");
            throw new WorkException(workCode);
        }
    }

    private static JSONObject getBaseJson(){
        JSONObject data = new JSONObject();
        data.put("sid", SmsConfig.SID);
        data.put("token", SmsConfig.TOKEN);
        data.put("appid", SmsConfig.APP_ID);
        return data;
    }
}
